package com.pinc.poop;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;

public class setting extends PwdProtectPreferenceActivity {
	private SharedPreferences settings;
	private SharedPreferences.Editor editor;

	private CheckBoxPreference cbp_pwd;
	private EditTextPreference etp_pwd1;
	private EditTextPreference etp_pwd2;

	private final int ID_ERR_FORMAT = 0;
	private final int ID_ERR_MISMATCH = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.addPreferencesFromResource(R.xml.setting);

		settings = PreferenceManager.getDefaultSharedPreferences(this);
		editor = settings.edit();

		/* Sex */
		ListPreference lp = (ListPreference) this.findPreference("user_sex");
		if ( settings.getString("user_sex", "null").compareTo("null") != 0)
			lp.setSummary(lp.getEntry());
		lp.setOnPreferenceChangeListener(new PrefChangeListener());

		/* Age */
		EditTextPreference etp = (EditTextPreference) this.findPreference("user_age");
		if ( settings.getString("user_age", "null").compareTo("null") != 0)
			etp.setSummary(etp.getText());
		etp.setOnPreferenceChangeListener(new PrefChangeListener());

		/* Period */
		EditTextPreference etp_period = (EditTextPreference) this.findPreference("user_noti_period");
		etp_period.setSummary(String.format("%d %s", settings.getInt("user_period_int", 48), getResources().getString(R.string.str_hours)));
		etp_period.setOnPreferenceChangeListener(new PeriodChangeListener());

		/* Vibration */

		/* Notify */
		CheckBoxPreference cbp = (CheckBoxPreference) this.findPreference("user_noti");
		cbp.setOnPreferenceChangeListener(new NotifyPrefChangeListener() );

		/* Password */
		cbp_pwd = (CheckBoxPreference) this.findPreference("pwd_protect");
		etp_pwd1 = (EditTextPreference) this.findPreference("password");
		etp_pwd2 = (EditTextPreference) this.findPreference("password2");

		etp_pwd1.setOnPreferenceChangeListener(new PwdPrefChangeListener());
		etp_pwd2.setOnPreferenceChangeListener(new Pwd2PrefChangeListener());
	}

	private class PrefChangeListener implements Preference.OnPreferenceChangeListener
	{
		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			preference.setSummary((String)newValue);
			return true;
		}
	}

	private class PeriodChangeListener implements Preference.OnPreferenceChangeListener
	{
		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			if ( ((String)newValue).length() == 0)
				return false;
			int value = Integer.parseInt((String)newValue);
			boolean f = false;

			if ( value >= 1 )
			{
				editor.putInt("user_period_int", value);
				editor.commit();
				f=true;
			}

			editor.putString("user_noti_period", String.format("%d", settings.getInt("user_period_int", 48)));
			editor.commit();

			preference.setSummary(String.format("%s %s", settings.getString("user_noti_period", "48"), getResources().getString(R.string.str_hours)));
			return f;
		}
	}

	private class NotifyPrefChangeListener implements Preference.OnPreferenceChangeListener
	{
		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			String tmpstr = String.valueOf(newValue);
			boolean newBool = (tmpstr.compareTo("true") == 0);

			if (newBool)
			{
				Intent intent = new Intent(setting.this, BroadcastService.class);
				startService(intent);
			}
			return true;
		}
	}

	private class PwdPrefChangeListener implements Preference.OnPreferenceChangeListener
	{
		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			String pwd_new = (String)newValue;
			if (pwd_new.length() != 4)
			{
				((EditTextPreference)preference).setText("");
				etp_pwd2.setText("");
				cbp_pwd.setChecked(false);
				showDialog(ID_ERR_FORMAT);
				return false;
			}
			else if ( pwd_new.compareTo(etp_pwd2.getText()) != 0)
			{
				etp_pwd2.setText("");
				cbp_pwd.setChecked(false);
			}
			return true;
		}
	}

	private class Pwd2PrefChangeListener implements Preference.OnPreferenceChangeListener
	{
		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			String pwd_new = (String)newValue;
			if (pwd_new.length() != 4)
			{
				((EditTextPreference)preference).setText("");
				cbp_pwd.setChecked(false);
				showDialog(ID_ERR_FORMAT);
				return false;
			}
			else if (pwd_new.compareTo(etp_pwd1.getText()) != 0)
			{
				((EditTextPreference)preference).setText("");
				cbp_pwd.setChecked(false);
				showDialog(ID_ERR_MISMATCH);
				return false;
			}
			return true;
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch(id)
		{
		case ID_ERR_FORMAT:
		{
			builder.setMessage(getResources().getString(R.string.str_dialog_format_err)).setCancelable(false)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
			break;
		}
		case ID_ERR_MISMATCH:
		{
			builder.setMessage(getResources().getString(R.string.str_dialog_mismatch_err)).setCancelable(false)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
			break;
		}
		}
		return builder.create();
	}
}
